package com.example.androidexample.settings.fragments;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.example.androidexample.R;
import com.example.basic.utils.PreferenceUtils;

import java.util.Arrays;

public class MainPreferences extends PreferenceFragment {

    private static final String TAG = "MainSettingsFragment";
    private static final String FEED_BACK_URL = "https://support.qq.com/products/260646";
    private static final String USER_IMG_NUMBER = "userImageNumber";

    private static final String ZCSD_SETTING_DISPLAY_GESTURE = "zcsd_setting_display_gesture";
    private static final String ZCSD_SETTING_DATA_PRIVACY = "zcsd_setting_data_privacy";
    private static final String ZCSD_SETTING_DEFAULT_BROWSER = "zcsd_setting_default_browser";
    private static final String ZCSD_SETTING_FEEDBACK = "zcsd_setting_feedback";
    private static final String ZCSD_SETTING_CHECK_UPDATES = "zcsd_setting_check_updates";
    private static final String ZCSD_SETTING_ABOUT = "zcsd_setting_about";

    private boolean isStartDefaultBrowser;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PreferenceUtils.addPreferencesFromResource(this, R.xml.main_setting_preferences);
//        findPreference(ZCSD_SETTING_CHECK_UPDATES).setLayoutResource(UpdateTipsCoordinator.INSTANCE.isNeedUpdate()?R.layout.preference_common_zcsd_for_update:R.layout.preference_common_zcsd);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getActivity().setTitle("设置");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (view != null) {
            Context context = view.getContext();
            ListView listView = view.findViewById(android.R.id.list);
            Drawable divider = ResourcesCompat.getDrawable(
                    context.getResources(),
                    R.drawable.list_divider,
                    context.getTheme());
            listView.setDivider(divider);
        }
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        String key = preference.getKey();
        Context context = preference.getContext();
        switch (key) {
            case ZCSD_SETTING_ABOUT:
//                AboutActivity.start(context);
                return true;

            case ZCSD_SETTING_CHECK_UPDATES:
//                UmengProxy.onEvent(context, EventConstant.CLICK_UPDATE);
//                if (mDownloadDelegate == null) {
//                    mDownloadDelegate = new ActiveDownloadDelegate(getActivity());
//                }
//                mDownloadDelegate.checkNewVersion();
//                if (UpdateTipsCoordinator.INSTANCE.isNeedUpdate()) {
//                    HashMap<String,Object> event = new HashMap<>();
//                    event.put(EventConstant.CLICK_CHECK_UPDATE_KEY,EventConstant.CLICK_CHECK_UPDATE_VALUE);
//                    UmengProxy.onObjectEvent(context, EventConstant.UPDATE_TIPS,event);
//                }
//                UpdateTipsCoordinator.INSTANCE.setNeedUpdate(false);
//                UpdateTipsCoordinator.INSTANCE.notifyUpdate();
                return true;

            case ZCSD_SETTING_FEEDBACK:
                //以设备id作为openid作为信息带入
//                int userImgNumber = ContextUtils.getAppSharedPreferences().getInt(USER_IMG_NUMBER, -1);
//                String deviceID = null;
//                String[] deviceInfo = null;
//                if (userImgNumber == -1) {
//                    Random random = new Random();
//                    userImgNumber =  random.nextInt(250)+ 1;
//                    ContextUtils.getAppSharedPreferences().edit().putInt(USER_IMG_NUMBER, userImgNumber).apply();
//                    deviceID = DeviceFeature.getShaDeviceId();
//                } else {
//                    deviceInfo = UMConfigure.getTestDeviceInfo(context);
//                    deviceID = TextUtils.isEmpty(deviceInfo[0]) ? deviceInfo[1] : deviceInfo[0];
//                }
//                String nickname = "游客" + getNickname(deviceID);
//                String avatar = "https://txc.gtimg.com/static/v2/img/avatar/"+userImgNumber+".svg";
//                CustomTabActivity.showInfoPage(context, FEED_BACK_URL + "?nickname=" + nickname + "&avatar=" + avatar + "&openid=" + deviceID);
                return true;

            case ZCSD_SETTING_DEFAULT_BROWSER:
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                    startActivity(new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS));
//                } else {
//                    Intent settingsIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                    settingsIntent.setData(Uri.parse("package:" +
//                            ContextUtils.getApplicationContext().getPackageName()));
//                    settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(settingsIntent);
//                }
//                UmengProxy.onEvent(context, EventConstant.CLICK_SET_DEFAULT);
//                isStartDefaultBrowser = true;
                return true;

            case ZCSD_SETTING_DATA_PRIVACY:
            case ZCSD_SETTING_DISPLAY_GESTURE:
                return super.onPreferenceTreeClick(preferenceScreen, preference);

            default:
                Log.e(TAG, "unknown preference key : " + key);
                return false;
        }
    }

    private static final int MAX_NICKNAME_LENGTH = 6;

    private String getNickname(String deviceID) {
        int length = deviceID.length();
        if (length < MAX_NICKNAME_LENGTH) {
            char[] supplementChars = new char[MAX_NICKNAME_LENGTH - length];
            Arrays.fill(supplementChars, 'x');
            return deviceID + supplementChars.toString();
        } else {
            return deviceID.substring(0, MAX_NICKNAME_LENGTH);
        }
    }
}
